package com.offcn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.offcn.dao.BlogMapper;
import com.offcn.dao.EvaluateMapper;
import com.offcn.pojo.Blog;
import com.offcn.pojo.EvaluateExample;
import com.offcn.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private EvaluateMapper evaluateMapper;

    //列表展示 -- 分页
    @Override
    public PageInfo<Blog> queryList(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Blog> blogs = blogMapper.selectByExample(null);
        PageInfo<Blog>pageInfo = new PageInfo<>(blogs,5);
        return pageInfo;
    }

    //删除操作
    @Override
    @Transactional
    public void delete(int id) {
        //要删除评论  和  博客
        //先删除外键约束的，再删除主键的
        EvaluateExample evaluateExample = new EvaluateExample();
        EvaluateExample.Criteria criteria = evaluateExample.createCriteria();
        criteria.andBFkEqualTo(id);

        evaluateMapper.deleteByExample(evaluateExample);

        blogMapper.deleteByPrimaryKey(id);
    }

    //博客详情
    @Override
    public Blog queryById(int id) {
        //有博客信息  有评论信息  博客的博主名称   评论的评论人名称
        return blogMapper.selectByPrimaryKey(id);
    }
}
